
package org.dydl.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Component
@Configuration
public class MvcConfigurationAdapter extends WebMvcConfigurerAdapter {
	@Autowired
	private StaticResourcesConfiguration configuration;

	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		super.addResourceHandlers(registry);
		registry.addResourceHandler(new String[] { "/public/**" })
				.addResourceLocations(new String[] { this.configuration.getAnonymous() });
		registry.addResourceHandler(new String[] { "/assets/**" })
				.addResourceLocations(new String[] { this.configuration.getLibs() });
		registry.addResourceHandler(new String[] { "/**" })
				.addResourceLocations(new String[] { this.configuration.getPermission() });

	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("forward:/main.html");
		registry.addViewController("/changepwd").setViewName("forward:/public/changepwd.html");
		registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
		super.addViewControllers(registry);
	}

	public void addInterceptors(InterceptorRegistry registry) {
		// 多个拦截器组成一个拦截器链
		// addPathPatterns 用于添加拦截规则
		// excludePathPatterns 用户排除拦截
		registry.addInterceptor(new LogHandlerInterceptor()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}
	
//	@Override
//	public void configureViewResolvers(ViewResolverRegistry registry) {
//		registry.enableContentNegotiation(new XlsxView());
//	}

	// @Bean
	// public EmbeddedServletContainerCustomizer containerCustomizer(){
	// return new MyCustomizer();
	// }
	//
	// // ...
	// private static class MyCustomizer implements
	// EmbeddedServletContainerCustomizer {
	// @Override
	// public void customize(ConfigurableEmbeddedServletContainer container) {
	//// container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
	//// container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED,
	// "/401"));
	//// container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403"));
	//// container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
	//// container.addErrorPages(new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED,
	// "/405"));
	//// container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,
	// "/500"));
	// }
	// }
	//
	// @Bean
	// public ServletRegistrationBean dispatcherRegistration() {
	// return new ServletRegistrationBean(dispatcherServlet());
	// }
	//
	// @Bean(name =
	// DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
	// public DispatcherServlet dispatcherServlet() {
	// DispatcherServlet ds = new DispatcherServlet();
	// ds.setDispatchTraceRequest(true);
	// ds.setThrowExceptionIfNoHandlerFound(true);
	// return ds;
	//// return new LoggableDispatcherServlet();
	// }

	@Bean
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setBasename("classpath:messages_zh_CN.properties");
		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}
	//
	// @Bean
	// public LocaleResolver localeResolver() {
	// return new AcceptHeaderLocaleResolver();
	// }
}